{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import codecs\n",
    "import requests\n",
    "import json\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import sys\n",
    "default_encoding = 'utf-8'\n",
    "if sys.getdefaultencoding() != default_encoding:\n",
    "    reload(sys)\n",
    "    sys.setdefaultencoding(default_encoding)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "r=requests.get(\"http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\")\n",
    "f=codecs.open('c:\\\\pythonCoding\\\\S1EP3_Iris.txt','w',encoding='utf-8')\n",
    "f.write(r.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "'''\n",
    "pd.read_csv:header=None,不然会把数据的第一行作为列索引，缺失一行数据\n",
    "header表示数据中是否存在列名，不存在就写None\n",
    "encoding 表示数据集的字符编码，通常一份数据为了方便的进行文件传输都以utf-8作为标准\n",
    "'''\n",
    "RequestData=pd.read_csv('c:\\\\pythonCoding\\\\S1EP3_Iris.txt',header=None,encoding='utf-8');\n",
    "#RequestData"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "RequestData.columns=['sepal_length','sepal_width','petal_length','petal_width','class'];\n",
    "#RequestData"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Iris-setosa        50\n",
       "Iris-versicolor    50\n",
       "Iris-virginica     50\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "RequestData['class'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Series_Dict={'American':'NY',\"France\":\"Pairs\",\"China\":\"BeiJing\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "American         NY\n",
       "China       BeiJing\n",
       "France        Pairs\n",
       "dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SeriesData=pd.Series(Series_Dict)\n",
    "SeriesData"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['NY', 'BeiJing', 'Pairs'], dtype=object)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " SeriesData.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "SeriesData1=pd.Series(SeriesData,index=[\"china\",\"American\",\"France\",\"England\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Row0  Col0     0\n",
       "      Col1     1\n",
       "      Col2     2\n",
       "      Col3     3\n",
       "Row1  Col0     4\n",
       "      Col1     5\n",
       "      Col2     6\n",
       "      Col3     7\n",
       "Row2  Col0     8\n",
       "      Col1     9\n",
       "      Col2    10\n",
       "      Col3    11\n",
       "Row3  Col0    12\n",
       "      Col1    13\n",
       "      Col2    14\n",
       "      Col3    15\n",
       "dtype: int32"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "series的复合索引\n",
    "'''\n",
    "mutil=pd.Index([(\"Row\"+str(x),\"Col\"+str(y)) for x in xrange(4) for y in xrange(4)])\n",
    "data1=pd.Series(xrange(0,16),index=mutil)\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Col0</th>\n",
       "      <th>Col1</th>\n",
       "      <th>Col2</th>\n",
       "      <th>Col3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Row0</th>\n",
       "      <td>  0</td>\n",
       "      <td>  1</td>\n",
       "      <td>  2</td>\n",
       "      <td>  3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Row1</th>\n",
       "      <td>  4</td>\n",
       "      <td>  5</td>\n",
       "      <td>  6</td>\n",
       "      <td>  7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Row2</th>\n",
       "      <td>  8</td>\n",
       "      <td>  9</td>\n",
       "      <td> 10</td>\n",
       "      <td> 11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Row3</th>\n",
       "      <td> 12</td>\n",
       "      <td> 13</td>\n",
       "      <td> 14</td>\n",
       "      <td> 15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Col0  Col1  Col2  Col3\n",
       "Row0     0     1     2     3\n",
       "Row1     4     5     6     7\n",
       "Row2     8     9    10    11\n",
       "Row3    12    13    14    15"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "MultiIndex的Series可以unstack()成DataFrame\n",
    "DataFrame可以stack成拥有双重MultiIndex的Series\n",
    "'''\n",
    "data1.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>job</th>\n",
       "      <th>name</th>\n",
       "      <th>report</th>\n",
       "      <th>sal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>      VP</td>\n",
       "      <td>  wang</td>\n",
       "      <td> NaN</td>\n",
       "      <td> 50000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> Manager</td>\n",
       "      <td> zhang</td>\n",
       "      <td>  VP</td>\n",
       "      <td>   NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>     NaN</td>\n",
       "      <td>   Lee</td>\n",
       "      <td>  IT</td>\n",
       "      <td>  5000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       job   name report    sal\n",
       "0       VP   wang    NaN  50000\n",
       "1  Manager  zhang     VP    NaN\n",
       "2      NaN    Lee     IT   5000"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "json_data=[{'name':'wang','sal':50000,'job':'VP'},{'name':'zhang','job':'Manager','report':'VP'},{'name':'Lee','sal':5000,'report':'IT'}]\n",
    "data_employee=pd.read_json(json.dumps(json_data))\n",
    "data_employee"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>job</th>\n",
       "      <th>sal</th>\n",
       "      <th>report</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name      job    sal report\n",
       "0   wang       VP  50000    NaN\n",
       "1  zhang  Manager    NaN     VP\n",
       "2    Lee      NaN   5000     IT"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_employee.reindex(columns=['name','job','sal','report'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>job</th>\n",
       "      <th>sal</th>\n",
       "      <th>report</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name      job    sal report\n",
       "0   wang       VP  50000    NaN\n",
       "1  zhang  Manager    NaN     VP\n",
       "2    Lee      NaN   5000     IT\n",
       "0   wang       VP  50000    NaN\n",
       "1  zhang  Manager    NaN     VP\n",
       "2    Lee      NaN   5000     IT"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_employee1=data_employee.reindex(columns=['name','job','sal','report'])\n",
    "data_employeeSum=pd.concat([data_employee1,data_employee1])\n",
    "data_employeeSum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>job_x</th>\n",
       "      <th>sal_x</th>\n",
       "      <th>report_x</th>\n",
       "      <th>job_y</th>\n",
       "      <th>sal_y</th>\n",
       "      <th>report_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name    job_x  sal_x report_x    job_y  sal_y report_y\n",
       "0   wang       VP  50000      NaN       VP  50000      NaN\n",
       "1  zhang  Manager    NaN       VP  Manager    NaN       VP\n",
       "2    Lee      NaN   5000       IT      NaN   5000       IT"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(data_employee1,data_employee1,on='name')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#数据的合并，整理\n",
    "横向连接---concat\n",
    "数据块A B C\n",
    "concat连接：========》A\n",
    "                      B\n",
    "                      C(ABC排成一列)\n",
    "\n",
    "纵向连接---merge\n",
    "数据块A B C\n",
    "merge连接：========》ABC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.29466354  0.53271311  0.95729857]\n",
      "[ 0.24678673  0.07176952  0.44165165  0.4210018 ]\n",
      "[ 0.26032399  0.76351393]\n",
      "         c1        c2        c3        c4\n",
      "0  0.294664  0.532713  0.957299       NaN\n",
      "1  0.246787  0.071770  0.441652  0.421002\n",
      "2  0.260324  0.763514       NaN       NaN\n"
     ]
    }
   ],
   "source": [
    "#单条数据的合并\n",
    "#---------------------------------------------------------\n",
    "r1=np.random.rand(3)\n",
    "r2=np.random.rand(4)\n",
    "r3=np.random.rand(2)\n",
    "\n",
    "df=pd.DataFrame([r1,r2,r3],columns=['c1','c2','c3','c4'])\n",
    "print r1\n",
    "print r2\n",
    "print r3\n",
    "print df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>job</th>\n",
       "      <th>sal</th>\n",
       "      <th>report</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name      job    sal report\n",
       "0   wang       VP  50000    NaN\n",
       "1  zhang  Manager    NaN     VP\n",
       "2    Lee      NaN   5000     IT\n",
       "0   wang       VP  50000    NaN\n",
       "1  zhang  Manager    NaN     VP\n",
       "2    Lee      NaN   5000     IT"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#一块一块数据的横向连接 concat\n",
    "json_data=[{'name':'wang','sal':50000,'job':'VP'},{'name':'zhang','job':'Manager','report':'VP'},{'name':'Lee','sal':5000,'report':'IT'}]\n",
    "data_employee=pd.read_json(json.dumps(json_data))\n",
    "data_employee1=data_employee.reindex(columns=['name','job','sal','report'])\n",
    "data_employeeSum=pd.concat([data_employee1,data_employee1])#《============在这里使用concat函数，实现横向连接\n",
    "data_employeeSum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>job</th>\n",
       "      <th>sal</th>\n",
       "      <th>report</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name      job    sal report\n",
       "0   wang       VP  50000    NaN\n",
       "1  zhang  Manager    NaN     VP\n",
       "2    Lee      NaN   5000     IT\n",
       "3   wang       VP  50000    NaN\n",
       "4  zhang  Manager    NaN     VP\n",
       "5    Lee      NaN   5000     IT"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#注意在上面的连接中，index同样被复制连接了，如果要忽略，需使用参数ignore_index\n",
    "pd.concat([data_employee1,data_employee1],ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>job_x</th>\n",
       "      <th>sal_x</th>\n",
       "      <th>report_x</th>\n",
       "      <th>job_y</th>\n",
       "      <th>sal_y</th>\n",
       "      <th>report_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name    job_x  sal_x report_x    job_y  sal_y report_y\n",
       "0   wang       VP  50000      NaN       VP  50000      NaN\n",
       "1  zhang  Manager    NaN       VP  Manager    NaN       VP\n",
       "2    Lee      NaN   5000       IT      NaN   5000       IT"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#纵向连接merge\n",
    "pd.merge(data_employee1,data_employee1,on=\"name\")#on参数指明按照哪个列名连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>job</th>\n",
       "      <th>sal_x</th>\n",
       "      <th>report_x</th>\n",
       "      <th>sal_y</th>\n",
       "      <th>report_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name      job  sal_x report_x  sal_y report_y\n",
       "0   wang       VP  50000      NaN  50000      NaN\n",
       "1  zhang  Manager    NaN       VP    NaN       VP\n",
       "2    Lee      NaN   5000       IT   5000       IT"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(data_employee1,data_employee1,on=[\"name\",\"job\"])#该种情况主要是在同一列名下，有相同名字时，可以使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name_x</th>\n",
       "      <th>job_x</th>\n",
       "      <th>sal_x</th>\n",
       "      <th>report_x</th>\n",
       "      <th>name_y</th>\n",
       "      <th>job_y</th>\n",
       "      <th>sal_y</th>\n",
       "      <th>report_y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  wang</td>\n",
       "      <td>      VP</td>\n",
       "      <td> 50000</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "      <td> zhang</td>\n",
       "      <td> Manager</td>\n",
       "      <td>   NaN</td>\n",
       "      <td>  VP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "      <td>   Lee</td>\n",
       "      <td>     NaN</td>\n",
       "      <td>  5000</td>\n",
       "      <td>  IT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name_x    job_x  sal_x report_x name_y    job_y  sal_y report_y\n",
       "index1                                                                \n",
       "0        wang       VP  50000      NaN   wang       VP  50000      NaN\n",
       "1       zhang  Manager    NaN       VP  zhang  Manager    NaN       VP\n",
       "2         Lee      NaN   5000       IT    Lee      NaN   5000       IT"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#merge 按照index关联时，DataFrame必须有index名\n",
    "data_employee1.index.name=\"index1\"\n",
    "pd.merge(data_employee1,data_employee1,left_index=\"index1\",right_index=\"index1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>salary</th>\n",
       "      <th>job</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td> 老王</td>\n",
       "      <td>  1000</td>\n",
       "      <td> guard</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> 老李</td>\n",
       "      <td> 25000</td>\n",
       "      <td>    VP</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  salary    job\n",
       "0   老王    1000  guard\n",
       "1   老李   25000     VP"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1=pd.DataFrame({'name':[u'老王',u'老李',u'老刘'],\"salary\":[1000,25000,20000]})\n",
    "df2=pd.DataFrame({'name':[u'老王',u'老李'],\"job\":[\"guard\",\"VP\"]})\n",
    "pd.merge(df1,df2,how=\"left\") #how=\"left\"按照左表即df1，合并\n",
    "pd.merge(df1,df2,how=\"right\") # how=\"right\"按照左表即df2，合并  \n",
    "#how=\"inner\"按交集合并，how=\"outer\"按并集合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#数据清洗的三剑客，map，applymap，apply"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将一个自定义的函数应用到Pandas的数据结构中可以使用map(), apply()或者applymap()，它们的区别在于应用的对象不同。\n",
    "\n",
    "1. map() 是一个Series的函数，DataFrame结构中没有map()。map()将一个自定义函数应用于Series结构中的每个元素(elements)。\n",
    "\n",
    "2. apply()和applymap()是DataFrame结构中的函数，Series中没有。它们的区别在于，apply()将一个函数作用于DataFrame中的每个行或者列（axis=0对列操作，默认情况下。axis=1对行操作），而applymap()是将函数做用于DataFrame中的所有元素(elements)。​"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nations</th>\n",
       "      <th>captial</th>\n",
       "      <th>GDP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>   china</td>\n",
       "      <td> beijing</td>\n",
       "      <td> 8000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> s.korea</td>\n",
       "      <td>   sesol</td>\n",
       "      <td> 2000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>  enland</td>\n",
       "      <td>  landon</td>\n",
       "      <td> 5000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   nations  captial   GDP\n",
       "0    china  beijing  8000\n",
       "1  s.korea    sesol  2000\n",
       "2   enland   landon  5000"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "npdata=np.asarray([(\"china\",\"beijing\",8000),(\"s.korea\",\"sesol\",2000),(\"enland\",\"landon\",5000)])\n",
    "df3=pd.DataFrame(npdata,columns=[\"nations\",\"captial\",\"GDP\"])\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nations</th>\n",
       "      <th>captial</th>\n",
       "      <th>GDP</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>   china</td>\n",
       "      <td> beijing</td>\n",
       "      <td> 8000</td>\n",
       "      <td>   high</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> s.korea</td>\n",
       "      <td>   sesol</td>\n",
       "      <td> 2000</td>\n",
       "      <td>    low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>  enland</td>\n",
       "      <td>  landon</td>\n",
       "      <td> 5000</td>\n",
       "      <td> medium</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   nations  captial   GDP    rank\n",
       "0    china  beijing  8000    high\n",
       "1  s.korea    sesol  2000     low\n",
       "2   enland   landon  5000  medium"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def GDP_factorize(a):\n",
    "    temp=np.float64(a)\n",
    "    if temp>5000.0:\n",
    "        return \"high\"\n",
    "    elif temp<3000.0:\n",
    "        return \"low\"\n",
    "    else:\n",
    "        return \"medium\"\n",
    "\n",
    "df3['rank']=df3['GDP'].map(GDP_factorize)#map中既可以是自建函数也可以是Python的内置函数\n",
    "df3\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nations</th>\n",
       "      <th>captial</th>\n",
       "      <th>GDP</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>   CHINA</td>\n",
       "      <td> BEIJING</td>\n",
       "      <td> 16000</td>\n",
       "      <td>   HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> S.KOREA</td>\n",
       "      <td>   SESOL</td>\n",
       "      <td>  4000</td>\n",
       "      <td>    LOW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>  ENLAND</td>\n",
       "      <td>  LANDON</td>\n",
       "      <td> 10000</td>\n",
       "      <td> MEDIUM</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   nations  captial    GDP    rank\n",
       "0    CHINA  BEIJING  16000    HIGH\n",
       "1  S.KOREA    SESOL   4000     LOW\n",
       "2   ENLAND   LANDON  10000  MEDIUM"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# applymap相当于是与DataFrame中的每一个元素都进行函数运算\n",
    "df3.applymap(lambda x: float(x)*2 if x.isdigit() else x.upper()) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#apply可以对DataFrame操作，而得到一个Series\n",
    "df3.apply(lambda x: x['captial']+'_'+x['GDP'],axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#！！！！！！！！排序！！！！！！！！！！！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nations</th>\n",
       "      <th>captial</th>\n",
       "      <th>GDP</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>   china</td>\n",
       "      <td> beijing</td>\n",
       "      <td> 8000</td>\n",
       "      <td>   high</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>  enland</td>\n",
       "      <td>  landon</td>\n",
       "      <td> 5000</td>\n",
       "      <td> medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> s.korea</td>\n",
       "      <td>   sesol</td>\n",
       "      <td> 2000</td>\n",
       "      <td>    low</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   nations  captial   GDP    rank\n",
       "0    china  beijing  8000    high\n",
       "2   enland   landon  5000  medium\n",
       "1  s.korea    sesol  2000     low"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.sort(\"GDP\",ascending=False)#列名与ascending不要分开写，分开写相当于是排名了两次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nations</th>\n",
       "      <th>captial</th>\n",
       "      <th>GDP</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>  enland</td>\n",
       "      <td>  landon</td>\n",
       "      <td> 5000</td>\n",
       "      <td> medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> s.korea</td>\n",
       "      <td>   sesol</td>\n",
       "      <td> 2000</td>\n",
       "      <td>    low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>   china</td>\n",
       "      <td> beijing</td>\n",
       "      <td> 8000</td>\n",
       "      <td>   high</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   nations  captial   GDP    rank\n",
       "2   enland   landon  5000  medium\n",
       "1  s.korea    sesol  2000     low\n",
       "0    china  beijing  8000    high"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.sort(\"GDP\").sort(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>c1</th>\n",
       "      <th>c2</th>\n",
       "      <th>c3</th>\n",
       "      <th>c4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td> 0.294664</td>\n",
       "      <td> 0.532713</td>\n",
       "      <td> 0.957299</td>\n",
       "      <td>      NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td> 0.246787</td>\n",
       "      <td> 0.071770</td>\n",
       "      <td> 0.441652</td>\n",
       "      <td> 0.421002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td> 0.260324</td>\n",
       "      <td> 0.763514</td>\n",
       "      <td>      NaN</td>\n",
       "      <td>      NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         c1        c2        c3        c4\n",
       "0  0.294664  0.532713  0.957299       NaN\n",
       "1  0.246787  0.071770  0.441652  0.421002\n",
       "2  0.260324  0.763514       NaN       NaN"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_index(axis=1,ascending=True)  # axis=1对行进行排序,axis=0对列进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'DataFrame' object has no attribute 'sort_values'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-b8e5d0542680>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mdf11\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mdf11\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mascending\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#进行行升序排列\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mdf11\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort_values\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mby\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'a'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m's'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\Anaconda\\lib\\site-packages\\pandas\\core\\generic.pyc\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m   2148\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2149\u001b[0m             raise AttributeError(\"'%s' object has no attribute '%s'\" %\n\u001b[1;32m-> 2150\u001b[1;33m                                  (type(self).__name__, name))\n\u001b[0m\u001b[0;32m   2151\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2152\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__setattr__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'sort_values'"
     ]
    }
   ],
   "source": [
    "df11 = pd.DataFrame(np.random.rand(3,4),index = [4,1,6],columns = list(\"adse\"))\n",
    "print df11\n",
    "print df11.sort_index(axis = 1,ascending=True) #进行行升序排列\n",
    "print df11.sort_values(by = ['a','s'])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## apply使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df10 = pd.DataFrame(np.random.rand(4,3),index = ['jiangxi','beijing','shanghai','tianjing'],columns = list('abc'))\n",
    "f = lambda x : x.max() - x.min()\n",
    "print df10\n",
    "#注意，默认axis=0，说明对列进行操作，axis=1对行进行操作\n",
    "print \"============\"\n",
    "print df10.apply(f)\n",
    "print \"============\"\n",
    "print df10.apply(f,axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
